package com.origin.multimedia.music.utils;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.MediaMetadataRetriever;
import android.provider.MediaStore;
import android.util.Log;

import com.origin.multimedia.music.bean.MusicInfo;
import com.origin.utils.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MusicUtils {

    /**
     * 扫描系统里面的音频文件，返回一个list集合
     */
    public static List<MusicInfo> getMusicData(Context context) {
        List<MusicInfo> list = new ArrayList<MusicInfo>();
        // 媒体库查询语句（写一个工具类MusicUtils）
        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
                null, MediaStore.Audio.Media.IS_MUSIC);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                MusicInfo musicInfo = new MusicInfo();
                //歌曲名称
                musicInfo.setSongName(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
                //歌手
                musicInfo.setArtist(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
                //专辑ID
//                song.albumId = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
                //专辑名
                musicInfo.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)));
                //歌曲路径
                musicInfo.setMusicUrl(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
                //歌曲时长
                musicInfo.setLength(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)) / 1000);
                //歌曲大小
                musicInfo.setSize(cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)));

                // 是否是网络音乐
                musicInfo.setNetMusic(false);
                musicInfo.setAlbumCover(getAlbumPicture(musicInfo.getMusicUrl(), context, musicInfo.getSongName()));
                if (musicInfo.getSize() > 1000 * 800) {
                    // 注释部分是切割标题，分离出歌曲名和歌手 （本地媒体库读取的歌曲信息不规范）
//                    if (song.song.contains("-")) {
//                        String[] str = song.song.split("-");
//                        song.singer = str[0];
//                        song.song = str[1];
//                    }

                    list.add(musicInfo);
                }
            }
            // 释放资源
            cursor.close();
        }
        return list;
    }


    public static String getAlbumPicture(String path, Context context, String name) {

        MediaMetadataRetriever mmr = new MediaMetadataRetriever();

        try {
            mmr.setDataSource(path);
            byte[] data = mmr.getEmbeddedPicture();
            if (data != null) {
                return FileUtils.saveFileToOpenFile(data, name + ".jpg", context);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }

        return "";

    }

}
